


//
//  ZMineViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/4/16.
//  Copyright © 2019年 admin. All rights reserved.

#import "ZSignViewController.h"
#import "ZReadHistoryViewController.h"
#import "ZLoginViewController.h"
#import "ZAgreementViewController.h"
#import "ZBookShelfViewController.h"
#import "ZStoreViewController.h"
#import "NFourIconView.h"
#import "ZMsgViewController.h"
#import "ZMineViewController.h"
#import "MyCell.h"
#import "MyHeadView.h"
#import "ZMemberViewController.h"
#import "MemberViewController.h"
#import "ZOrderViewController.h"
#import "ZInviteViewController.h"
#import "ZLikeViewController.h"
#import "ZFankuiViewController.h"
#import "ZSetViewController.h"
#import "ZUserMsgViewController.h"
#import "ZPadViewController.h"
#import "ZMyMasterpieceViewController.h"
////*************************播放器相关
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>
#import "UIImageView+ZFCache.h"
#import "ZFUtilities.h"


@interface ZMineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *titles;
@property (nonatomic,strong)NSArray *pics;
@property (nonatomic,strong)MyHeadView *headView;
@property (nonatomic,strong)NFourIconView *iconView;
@property (nonatomic,strong)NSMutableArray *userDataArray;

@end

#define cellIdentifier @"my"

@implementation ZMineViewController

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , -StatusBarHeight, ScreenWidth , ScreenHeight-TabBarHeight+StatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 50;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [UserModel shareInstance].isMine = YES;
    NSString *nick = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickName"];
    NSString *tel = [[NSUserDefaults standardUserDefaults]objectForKey:@"phone"];
    
    ///头像存在沙盒
    
    NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"head"];
    self.headView.jifenLab.text = [NSString stringWithFormat:@"%ld",(long)[[UserInfoSave shareInstance].integral integerValue]];
    self.headView.nickNameLab.text = [UserInfoSave shareInstance].nickName?:nick;
    self.headView.telLab.text = [UserInfoSave shareInstance].phone?:tel;
    ///
    self.headView.headImg.layer.cornerRadius = 29.f;
    self.headView.headImg.layer.masksToBounds = YES;
    
    if (data) {
        
        self.headView.headImg.image = [UIImage imageWithData:data];
        
    }else{
        
        
        if ([UserInfoSave shareInstance].photo) {
            
            [self.headView.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,[UserInfoSave shareInstance].photo]]];
            
        }else{
            self.headView.headImg.image = WDImgName(@"touxiang");
        }
        
    }
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headAction:)];
    [self.headView.headImg addGestureRecognizer:tap];
    
}

-(void)headAction:(UITapGestureRecognizer *)tap{

    ZUserMsgViewController *user = [ZUserMsgViewController new];
    [self.navigationController pushViewController:user animated:YES];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [self.navigationController setNavigationBarHidden:NO animated:NO];
    [UserModel shareInstance].isMine = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = nil;
    
    //我的页面
    [self.navigationItem setItemWithTitle:@"我的" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    
//  self.navigationController.navigationBar.shadowImage = [UIImage new];
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 45, 45) title:@"" Image:@"xiaoxi" selectedIMG:@"" tartget:self action:@selector(tapXiaoxi:)];
    self.userDataArray = [NSMutableArray array];
    ///数组嵌套数组
    self.titles = @[@[@"订单",@"会员",],@[@"邀请有奖",@"收藏",@"学生数据中心"],@[@"个人信息",@"意见反馈"],@[@"设置",]];
    self.pics = @[@[@"dingdan",@"huanguan(2)",],@[@"yaoqing",@"shoucang",@"lishiyuedu"],@[@"gerenxinxi",@"yijian"],@[@"shezhi",]];
//    self.titles = @[@[@"订单",],@[@"邀请有奖",@"收藏",@"学生数据中心"],@[@"个人信息",@"意见反馈"],@[@"设置",]];
//    self.pics = @[@[@"dingdan",],@[@"yaoqing",@"shoucang",@"lishiyuedu"],@[@"gerenxinxi",@"yijian"],@[@"shezhi",]];
    [self.view addSubview:self.tableView];
//    [self ConfigVideo];
    ///xib 头部视图
    
    MyHeadView *head =  [[[NSBundle mainBundle] loadNibNamed:@"MyHeadView" owner:self options:nil] lastObject];
    head.frame = CGRectMake(0, 0, ScreenWidth, 360);
    self.headView = head;
    [head.xiaoxiBtn addTarget:self action:@selector(tapmsg:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = head;
    [self addIconView];
}

- (void)addIconView {
    
    WeakStament(wself);
    
    self.iconView = [[NFourIconView alloc]initWithFrame:CGRectMake(0,290, ScreenWidth, fourIconHight)];
    self.iconView.iconArray = @[@"shujia",@"duihuan",@"qiandao",@"zuopin",@"pingban"];
    self.iconView.titleArray = @[@"书架",@"兑换商场",@"签到",@"我的作品",@"平板信息"];
    self.iconView.indexBlock = ^(NSInteger indexNum, NFourIconModel *model) {
        YYLog(@"%ld",(long)indexNum);
       
        [wself jumpToVC:indexNum];
    };
    [self.headView addSubview:self.iconView];
}

- (void)jumpToVC:(NSInteger)indexNum{
    switch (indexNum) {
        case 0:
        {
            //书架
            ZBookShelfViewController *shelf = [ZBookShelfViewController new];
            [self.navigationController pushViewController:shelf animated:YES];
            
        }
            break;
        case 1:
        {
            //商城
            ZStoreViewController *store = [ZStoreViewController new];
            [self.navigationController pushViewController:store animated:YES];
            
        }
            break;
        case 2:
        {
            //签到
            ZSignViewController *log = [ZSignViewController new];
            [self.navigationController pushViewController:log animated:YES];
//            [AFToast showText:@"已签到"];
        }
            break;
        case 3:
        {
            //我的作品
            ZMyMasterpieceViewController *master = [ZMyMasterpieceViewController new];
            [self.navigationController pushViewController:master animated:YES];
            
        }
            break;
        case 4:
        {
            //
            ZPadViewController *pad = [ZPadViewController new];
            [self.navigationController pushViewController:pad animated:YES];
            
        }
            break;
            
        default:
            break;
    }
}

///消息按钮点击事件
- (void)tapmsg:(UIButton *)btn{
    
    YYLog(@"点击消息");
    ZMsgViewController *msg = [ZMsgViewController new];
    [self.navigationController pushViewController:msg animated:YES];
    
}

#pragma mark - datasource


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        ///
        return [self.titles[0] count];
    }else if (section==1){
        return [self.titles[1] count];
    }else if (section==2){
        return [self.titles[2] count];
    }else{
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.title.text = self.titles[indexPath.section][indexPath.row];
    cell.iconImage.image = WDImgName(self.pics[indexPath.section][indexPath.row]);
    return cell;
}



#pragma mark delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}


#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        if (indexPath.row==0) {
            
            ZOrderViewController *ord = [ZOrderViewController new];
            [self.navigationController pushViewController:ord animated:YES];
            
        }else{
            MemberViewController *mem = [[MemberViewController alloc]init];
            [self.navigationController pushViewController:mem animated:YES];
        }
    }else if (indexPath.section==1){
        //1区
        if (indexPath.row==0) {
            ZInviteViewController *invite = [[ZInviteViewController alloc]initWithNibName:@"ZInviteViewController" bundle:nil];
            [self.navigationController pushViewController:invite animated:YES];
        }else if (indexPath.row==1){
            ZLikeViewController *like = [ZLikeViewController new];
            [self.navigationController pushViewController:like animated:YES];
        }else{
            ZReadHistoryViewController *data = [[ZReadHistoryViewController alloc]init];
            [self.navigationController pushViewController:data animated:YES];
        }
    }else if(indexPath.section==2){
        if (indexPath.row==0) {
            ZUserMsgViewController *msg = [ZUserMsgViewController new];
            [self.navigationController pushViewController:msg animated:YES];
        }else if (indexPath.row==1){
            ZFankuiViewController *fankui = [ZFankuiViewController new];
            [self.navigationController pushViewController:fankui animated:YES];
        }
    }else{
        ZSetViewController *set = [ZSetViewController new];
        [self.navigationController pushViewController:set animated:YES];
    }
}



@end
